import 'package:flutter/material.dart';
import '../utils/app_theme.dart';

class ReminderScreen extends StatefulWidget {
  final List<String> initialReminders;
  
  const ReminderScreen({
    super.key,
    this.initialReminders = const [],
  });

  @override
  State<ReminderScreen> createState() => _ReminderScreenState();
}

class _ReminderScreenState extends State<ReminderScreen> {
  late List<String> _reminders;
  final TextEditingController _customTimeController = TextEditingController();
  
  @override
  void initState() {
    super.initState();
    _reminders = List.from(widget.initialReminders);
  }
  
  @override
  void dispose() {
    _customTimeController.dispose();
    super.dispose();
  }
  
  void _addTimeReminder() async {
    final TimeOfDay? pickedTime = await showTimePicker(
      context: context,
      initialTime: TimeOfDay.now(),
    );
    
    if (pickedTime != null) {
      // Format time as HH:MM
      final String formattedTime = '${pickedTime.hour.toString().padLeft(2, '0')}:${pickedTime.minute.toString().padLeft(2, '0')}';
      
      if (!_reminders.contains(formattedTime)) {
        setState(() {
          _reminders.add(formattedTime);
          _reminders.sort(); // Sort times chronologically
        });
      }
    }
  }
  
  void _addCustomReminder() {
    if (_customTimeController.text.isNotEmpty &&
        !_reminders.contains(_customTimeController.text)) {
      setState(() {
        _reminders.add(_customTimeController.text);
        _customTimeController.clear();
      });
    }
  }
  
  void _removeReminder(String time) {
    setState(() {
      _reminders.remove(time);
    });
  }
  
  void _saveReminders() {
    Navigator.of(context).pop(_reminders);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('设置提醒'),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          onPressed: () => Navigator.of(context).pop(),
        ),
        actions: [
          TextButton(
            onPressed: _saveReminders,
            child: const Text('保存', style: TextStyle(color: Colors.white)),
          ),
        ],
      ),
      body: Column(
        children: [
          Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const Text(
                  '添加提醒时间',
                  style: AppTheme.subheadingStyle,
                ),
                const SizedBox(height: 16),
                
                // Add time picker
                ElevatedButton.icon(
                  onPressed: _addTimeReminder,
                  icon: const Icon(Icons.access_time),
                  label: const Text('选择时间'),
                  style: AppTheme.primaryButtonStyle,
                ),
                const SizedBox(height: 16),
                
                // Custom reminder input
                Row(
                  children: [
                    Expanded(
                      child: TextFormField(
                        controller: _customTimeController,
                        decoration: AppTheme.inputDecoration(
                          '自定义提醒',
                          hintText: '例如：早餐后、睡前',
                        ),
                      ),
                    ),
                    const SizedBox(width: 8),
                    IconButton(
                      onPressed: _addCustomReminder,
                      icon: const Icon(Icons.add_circle),
                      color: AppTheme.primaryColor,
                    ),
                  ],
                ),
              ],
            ),
          ),
          
          const Divider(),
          
          Expanded(
            child: _reminders.isEmpty
                ? const Center(
                    child: Text(
                      '没有设置提醒',
                      style: TextStyle(color: AppTheme.textSecondaryColor),
                    ),
                  )
                : ListView.separated(
                    padding: const EdgeInsets.all(16),
                    itemCount: _reminders.length,
                    separatorBuilder: (context, index) => const Divider(),
                    itemBuilder: (context, index) {
                      final reminder = _reminders[index];
                      return ListTile(
                        leading: const Icon(Icons.alarm),
                        title: Text(reminder),
                        trailing: IconButton(
                          icon: const Icon(Icons.delete_outline, color: Colors.red),
                          onPressed: () => _removeReminder(reminder),
                        ),
                      );
                    },
                  ),
          ),
        ],
      ),
    );
  }
}
